package cn.tedu.movie.controller;

import cn.hutool.json.JSONObject;
import cn.tedu.movie.entity.*;
import cn.tedu.movie.mapper.MovMapper;
import cn.tedu.movie.mapper.MovieMapper;
import jdk.nashorn.internal.ir.LiteralNode;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/movie/")
public class MovieController {
    private static final String DIRPath = "C:/upload/movie";
    @Autowired(required = false)
    MovieMapper movieMapper;
    @Autowired(required = false)
    MovMapper movMapper;

    @GetMapping("sessionMovie")
    public Movie sessionMovie(Integer id) {
        return movieMapper.sessionMovie(id);
    }

    @GetMapping("movielistByStatusId")
    public List<MovieByStatusId> movielistByStatusId(Integer movieStatusId) {
        return movieMapper.movielistByStatusId(movieStatusId);
    }

    @GetMapping("getCategory")
    List<Category> getCategory() {
        return movieMapper.getCategory();
    }

    @GetMapping("getRegion")
    List<Region> getRegion() {
        return movieMapper.getRegion();
    }

    @GetMapping("getMovieStatus")
    List<MovieStatus> getMovieStatus() {
        return movieMapper.getMovieStatus();
    }

    @GetMapping("getMovieList")
    List<MovieByStatusId> getMovieList(Parameter parameter) {
        return movMapper.getMovieList(parameter);
    }

    @GetMapping("searchByName")
    List<MovieByStatusId> searchByName(String name) {
        List<MovieByStatusId> movieList = movieMapper.searchByName(name);
        if (movieList.isEmpty()) {
            movieList = movieMapper.searchActorByActorName(name);
        }
        return movieList;
    }

    //    ---------------------------后端----------------------------------------
    @GetMapping("movielist")
    List<Movie> movieList(Integer page, Integer limit,String name) {
        if (name==null)
            name="";
        name=name.trim();
        List<Movie> list = movieMapper.movielist((page - 1) * limit, limit,name);
        if (list.size()>0)
        list.get(0).setCount(movieMapper.movieCount(name));
        return list;
    }

    @PostMapping("insertMovie")
    int insertMovie(MovieVo movieVo, MultipartFile picFile) throws IOException {
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        //得到和日期相关的路径
        SimpleDateFormat format =
                new SimpleDateFormat("/yyyy/MM/dd/");
        //把当前时间按照指定的格式进行转换 /2021/10/19/
        String datePath = format.format(new Date());
        //得到保存图片文件夹的路径f:/upload/vrd/2021/10/19/
        File dirFile = new File(DIRPath + datePath);
        //如果不存在则创建
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        //得到完成的文件路径
        String filePath = DIRPath + datePath + fileName;
        //保存上传的文件
        picFile.transferTo(new File(filePath));
        movieVo.setUrl(datePath + fileName);
        int num = movieMapper.insertMovie(movieVo);
        return num;
    }

    @GetMapping("deleteById")
    public int deleteById(Integer id) {
        String url = movieMapper.selectUrlById(id);
        String filePath = DIRPath + url;
        new File(filePath).delete();
        int num = movieMapper.deleteById(id);
        return num;
    }

    @PostMapping("deleteByIds")
    public void deleteByIds(String ids) {
        String[] strs = ids.split("[\\[\\],]");
        for (String str : strs) {
            if (!str.isEmpty()){
                Integer id=Integer.parseInt(str);
                String url = movieMapper.selectUrlById(id);
                String filePath = DIRPath + url;
                new File(filePath).delete();
                int num = movieMapper.deleteById(id);
            }
        }
    }
    @GetMapping("backstageSelectOne")
    public Movie selectOne(Integer id){
        return movieMapper.selectOne(id);
    }
    @PostMapping("updateMovie")
    public int updateMovie(Integer status,Integer id){
        return movieMapper.updateMovie(status,id);
    }
    @GetMapping("actorsByMovieId")
    public List<Actor> actorsByMovieId(Integer id){
        return movieMapper.actorsByMovieId(id);
    }
    @GetMapping("deleteCast")
    public int deleteCast(Integer actorId,Integer movieId){
        return movieMapper.deletCast(actorId,movieId);
    }
    @GetMapping("getActorNameById")
    public Integer getActorNameById(String name){
        return movieMapper.getActorNameById(name);
    }
    @GetMapping("addCast")
    public int addCast(Integer actorId,Integer movieId){
        return movieMapper.addCast(actorId,movieId);
    }
    @GetMapping("checkMovie")
    public Integer checkMovie(String name){
        return movieMapper.selectByName(name);
    }
}
